package com.thesis.manage.service.impl;

import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.service.impl.BaseServiceImpl;
import com.thesis.manage.domain.*;
import com.thesis.manage.mapper.StudentTeacherMapper;
import com.thesis.manage.service.*;
import com.thesis.manage.service.constant.ErrorMessage;
import com.thesis.manage.service.model.DocumentEnum;
import com.thesis.manage.service.model.ExamineEnum;
import com.thesis.manage.service.module.vo.DocumentStatusVO;
import com.thesis.manage.service.module.vo.StudentVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import tk.mybatis.mapper.common.Mapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author php
 * @date 2018/2/24
 */
@Service
public class StudentTeacherServiceImpl extends BaseServiceImpl<StudentTeacher> implements StudentTeacherService {
    @Resource
    private StudentTeacherMapper studentTeacherMapper;
    @Resource
    private StudentService studentService;
    @Resource
    private StudentThesisService studentThesisService;
    @Resource
    private DocumentInfoService documentInfoService;
    @Resource
    private ThesisService thesisService;

    @Override
    protected Mapper<StudentTeacher> getMapper() {
        return this.studentTeacherMapper;
    }

    @Override
    protected Class<StudentTeacher> getEntityClass() {
        return StudentTeacher.class;
    }

    @Override
    public List<StudentTeacher> findAll(StudentTeacher studentTeacher) {
        studentTeacher.setDelStatus(YesOrNoEnum.NO.getValue());
        return this.getMapper().select(studentTeacher);
    }

    @Override
    public List<StudentVO> findAllByTeacherId(StudentTeacher studentTeacher) {
        List<StudentVO> result = new ArrayList<>();
        List<StudentTeacher> studentTeachers = this.findAll(studentTeacher);
        //studentTeacher convert to studentVo
        if (CollectionUtils.isNotEmpty(studentTeachers)) {
            StudentVO studentVO;
            for (StudentTeacher st : studentTeachers) {
                //通过学生号长查询
                Student student = this.studentService.findByStudentNumber(st.getStudentNumber());
                if (student != null) {
                    studentVO = new StudentVO();
                    studentVO.setId(student.getId());
                    studentVO.setCollegeName(student.getCollegeName());
                    studentVO.setImage(student.getImage());
                    studentVO.setName(student.getName());
                    studentVO.setStudentNumber(student.getStudentNumber());
                    studentVO.setMajorName(student.getMajorName());
                    result.add(studentVO);
                }
            }
        }
        return result;
    }

    @Override
    public DocumentStatusVO findStudentThesisByStudentId(Long id) {
        DocumentStatusVO documentStatusVO = new DocumentStatusVO();
        StudentThesis studentThesis = new StudentThesis();
        studentThesis.setStudentId(id);
        //查有效的数据
        studentThesis.setInvalid(YesOrNoEnum.YES.getValue());
        StudentThesis one = this.studentThesisService.findByOne(studentThesis);
        if (one != null) {
            Thesis thesis = this.thesisService.findById(one.getThesisId());
            if (thesis == null) {
                throw new CommonException("thesis not exist", "论文题目不存在，请联系指导老师");
            }
            documentStatusVO.setThesisId(one.getThesisId());
            documentStatusVO.setThesisName(thesis.getName());
            documentStatusVO.setId(one.getId());
            documentStatusVO.setThesisExamine(one.getStatus() == ExamineEnum.TEACHER_EXAMINEING.getValue().intValue() ? "未审核" : "已审核");
            if (one.getStatus() == ExamineEnum.TEACHER_EXAMINEING.getValue().intValue()) {
                return documentStatusVO;
            } else {
                //查询任务书
                DocumentInfo documentInfo = this.documentInfoService.findByStudentId(id, DocumentEnum.TASK_BOOK.getValue());
                documentStatusVO.setTaskBookId(documentInfo == null ? null : documentInfo.getId());
                //查询开题报告
                DocumentInfo openReport = this.documentInfoService.findByStudentId(id, DocumentEnum.OPEN_REPORT.getValue());
                documentStatusVO.setOpenReportId(openReport == null ? null : openReport.getId());
                //查询中期报告
                DocumentInfo midTerm = this.documentInfoService.findByStudentId(id, DocumentEnum.MID_TERM_EXAMINATION.getValue());
                documentStatusVO.setMidtermExaminationId(midTerm == null ? null : midTerm.getId());
                //查询指导记录
                DocumentInfo guideRecord = this.documentInfoService.findByStudentId(id, DocumentEnum.GUIDE_RECORD.getValue());
                documentStatusVO.setGuideRecordId(guideRecord == null ? null : guideRecord.getId());
                //查询外文翻译
                DocumentInfo foreignLanguage = this.documentInfoService.findByStudentId(id, DocumentEnum.FOREIGN_LANGUAGE.getValue());
                documentStatusVO.setForeignLanguageId(foreignLanguage == null ? null : foreignLanguage.getId());
                //查询文献综述
                DocumentInfo literature = this.documentInfoService.findByStudentId(id, DocumentEnum.LITERATURE.getValue());
                documentStatusVO.setLiteratureId(literature == null ? null : literature.getId());
                //查询毕业设计论文
                DocumentInfo thesisInfo = this.documentInfoService.findByStudentId(id, DocumentEnum.THESIS_INFO.getValue());
                documentStatusVO.setThesisInfoId(thesisInfo == null ? null : thesisInfo.getId());
            }
        }
        return documentStatusVO;
    }

    @Override
    public StudentTeacher findByStudentNumber(String studentNumber) {
        Assert.notNull(studentNumber, ErrorMessage.PARAMS_IS_NULL);
        StudentTeacher studentTeacher = new StudentTeacher();
        studentTeacher.setStudentNumber(studentNumber);
        return this.findByOne(studentTeacher);
    }
}
